﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITJZ.BitHelper.APIModel;
using ITJZ.BitHelper.Local.UI.IComponent;

namespace ITJZ.BitHelper.Local.UI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AppRun();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (show.Visible)
            {
                show.Visible = false;
                edit.Visible = true;
                ((LinkLabel)sender).Text = "<<查询";

            }
            else
            {

                show.Visible = true;
                edit.Visible = false;
                ((LinkLabel)sender).Text = "新建>>";
            }
        }


        User user = null;//当前用户

        ISearchKnowledge iSearch = null;
        IEditorKnowledge iEditor = null;
        IShowKnowledge iShow = null;


        //填充组件
        UserControl search = Component.SearchComFactory.Create();
        UserControl edit = Component.EditorComFactory.Create();
        UserControl show = Component.ShowComFactory.Create();

        /// <summary>
        /// 程序启动
        /// </summary>
        public void AppRun()
        {

            iSearch = search as ISearchKnowledge;
            iEditor = edit as IEditorKnowledge;
            iShow = show as IShowKnowledge;

            //完成事件订阅
            iSearch.SelectChange += new EventHandler(search_SelectChange);
            iEditor.Save += new EventHandler(editor_Save);
            iEditor.Close += new EventHandler(editor_Close);

            edit.Visible = false;


            this.search.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
                      | System.Windows.Forms.AnchorStyles.Left)
                      | System.Windows.Forms.AnchorStyles.Right)));

            show.Height = this.splitContainer1.Panel2.Height;
            show.Width = this.splitContainer1.Panel2.Width;

            this.show.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
                      | System.Windows.Forms.AnchorStyles.Left)
                      | System.Windows.Forms.AnchorStyles.Right)));


            edit.Height = this.splitContainer1.Panel2.Height;
            edit.Width = this.splitContainer1.Panel2.Width;
            this.edit.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
                      | System.Windows.Forms.AnchorStyles.Left)
                      | System.Windows.Forms.AnchorStyles.Right)));

            this.splitContainer1.Panel1.Controls.Add(search);
            this.splitContainer1.Panel2.Controls.Add(edit);
            this.splitContainer1.Panel2.Controls.Add(show);            


            

        }

        void CreateShowControl()
        {
            show.Hide();

            var size = show.Size;

            show = Component.ShowComFactory.Create(iSearch.Language);

            show.Size = size;
            iShow = show as IShowKnowledge;


            this.show.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
          | System.Windows.Forms.AnchorStyles.Left)
          | System.Windows.Forms.AnchorStyles.Right)));

            this.splitContainer1.Panel2.Controls.Add(show);

            

            show.Show();
        }

        #region 事件处理函数：搜索组件
        private void search_SelectChange(object sender, EventArgs e)
        {
            CreateShowControl();

            string guid = iSearch.KnowledgeGuid;
         
            //获取到这个知识信息
            API.Knowledge akl = new ITJZ.BitHelper.Local.API.Knowledge();
            APIModel.Knowledge knowledge = akl.GetKnowledge(guid);

            //////////////////////////////////////////
            //【此转换仅用于当前状况】
            knowledge.Content = System.Web.HttpUtility.HtmlDecode(knowledge.Content);
            /////////////////////////////////////////

            iShow.Show(knowledge);
        } 
        #endregion

        #region 事件处理函数：知识编辑器组件
        private void editor_Save(object sender, EventArgs e)
        {
            Knowledge lk = iEditor.CurKnowledge;
            lk.Language = iSearch.Language;
            lk.Library = iSearch.Library;

            //提交这个知识到服务器并在本地数据库保存副本
            API.Knowledge api = new ITJZ.BitHelper.Local.API.Knowledge();
           ResInfo resinfo= api.Add(lk);
           if (resinfo.ErrState == 0)
           {
               MessageBox.Show(resinfo.Msg);
               iEditor.Hide();//隐藏编辑组件
               iShow.Show(lk);//显示刚才编辑的内容
               show.Visible = true;

               linkLabel1.Text = "新建>>";
           }
           else
           {

               MessageBox.Show(resinfo.Msg);
           }
        }
        private void editor_Close(object sender, EventArgs e)
        {
            iEditor.Hide();
            iShow.Show();
        } 
        #endregion

        private void frmMain_Resize(object sender, EventArgs e)
        {
            this.splitContainer1.SplitterDistance = 380;

            //edit.Height = this.splitContainer1.Panel2.Height;
            //edit.Width = this.splitContainer1.Panel2.Width;
        }
    }
}
